<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			/* 
				给定两个字符串 s 和 t，它们只包含小写字母。
				
				字符串  t  由字符串  s  随机重排，然后在随机位置添加一个字母。
				
				请找出在 t 中被添加的字母。
			 */
			let s = "abcd",
				t = "abcde";

			let findTheDifference = function(s, t) {
				let mapS = createCharMap(s);
				let mapT = createCharMap(t);
				
				let targetMap = mapS.size > mapT.size ? mapS : mapT;
				let otherMap = targetMap === mapS ? mapT : mapS;
				
				for (let key of targetMap.keys()){
					if (targetMap.get(key) !== otherMap.get(key)){
						return key;
					}
				}
			}

			findTheDifference(s, t);

			function createCharMap(s) {
				let map = new Map();

				for (let key of s) {
					let count = map.get(key);
					if (count) {
						map.set(key, count + 1);
					} else {
						map.set(key, 1);
					}
				}
				return map;
			}
		</script>
	</body>
</html>
